﻿#include "pch.h"
#include <windows.h>
#include <WinUser.h>
#include <iostream>

HHOOK g_hKeyboardHook;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
    if (nCode == HC_ACTION)
    {
        //p->flags = 144;
        //printf("p->vkCode: %d\n", p->vkCode);
        //printf("p->scanCode: %u\n", p->scanCode);
        //printf("p->flags: %u\n", p->flags);
        //printf("p->time: %u\n", p->time);

        switch (wParam)
        {
            case WM_KEYDOWN:
            case WM_SYSKEYDOWN:
                printf("vkCode:  0x%x\n", p->vkCode);
                break;
            case WM_KEYUP:
            case WM_SYSKEYUP:
                //std::cout << "弹起" << p->vkCode << std::endl;
                break;
        }
    }
    return CallNextHookEx(g_hKeyboardHook, nCode, wParam, lParam);
}

int main()
{
    g_hKeyboardHook = ::SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyboardProc, NULL, NULL); //设置全局钩子监听键盘事件

    MSG msg;
    while (::GetMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST))
    {
        PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)msg.lParam;
        ::TranslateMessage(&msg);
        ::DispatchMessage(&msg);
    }

    ::UnhookWindowsHookEx(g_hKeyboardHook);

    return 0;
}
